package com.example.ncov_management_system.controller;

import com.example.ncov_management_system.entity.Message;
import com.example.ncov_management_system.entity.Student;
import com.example.ncov_management_system.entity.Teacher;
import com.example.ncov_management_system.service.MessageService;
import com.example.ncov_management_system.service.StudentService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/message")
@Slf4j
public class MessageController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private StudentService studentService;

    @PostMapping("/add")
    @ResponseBody
    public Object add(long student_id, HttpSession session) {
        HashMap<String, Object> map = new HashMap<>();
        if (messageService.isRemind(student_id)) {
            map.put("code", 500);
            map.put("msg", "今日您已提醒过他");
            log.info("今日已醒过");
            return map;
        } else {
            Teacher teacher = (Teacher) session.getAttribute("teacher");
            Student student = studentService.findById(student_id);
            messageService.add(student, teacher);
            map.put("code", 200);
            map.put("msg", "提醒成功");
            log.info("提醒成功");
            return map;
        }
    }

    @PostMapping("/add_all")
    @ResponseBody
    public Object addAll(long[] student_ids, HttpSession session) {
        HashMap<String, Object> map = new HashMap<>();
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        for (long student_id : student_ids) {
            if (!messageService.isRemind(student_id)) {
                Student student = studentService.findById(student_id);
                messageService.add(student, teacher);
            }
        }
        map.put("code", 200);
        map.put("msg", "提醒成功");
        return map;
    }

    @GetMapping("/readed")
    @ResponseBody
    public Object readed(@RequestParam long id,HttpSession session) {
        messageService.readed(id);
        Student student = (Student) session.getAttribute("student");
        List<Message> messages = messageService.findUnReadByStudentId(student.getId());
        session.setAttribute("messages",messages);
        session.setAttribute("message_count",messages.size());
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "关闭成功");
        return map;
    }

    @GetMapping("/list")
    public String list(@RequestParam(name = "pn", defaultValue = "0") int pn, Model model,HttpSession session) {
        Student student = (Student) session.getAttribute("student");
        model.addAttribute("page", messageService.list(pn,student.getId()));
        return "student_message_list";
    }
}
